/**
 * Created by Administrator on 2018/5/9.
 * 操作的公共js
 */

function emptyStringConvert(str) {
    if (null == str || '' == str) {
        return '';
    } else {
        return str;
    }
};

/**
 *  打开更新节点界面
 *   tableid table的id
 *   id   数据id
 *   孙上峰
 */
function openUpdateNode(tableid, id) {
    if (null != tableid && "" != tableid) {
        var id = scCommonGetCheckedallproorder(tableid);
        if (id.length > 1) {
            DGG.Layer.message('只能选择一个订单！', {icon: '2'});
            return;
        }
        if (id.length == 0) {
            DGG.Layer.message('请选择一个订单！', {icon: '2'});
            return;
        }
        //判断该条数据弹出那个业态的界面
        $.ajax({
            type: 'POST',
            async: false,
            url: sysInfo.basePath + '/sc/general/inprocess/validationUpdateNode.do?id=' + id,
            dataType: 'JSON',
            success: function (obj) {
                if (obj.code === 0) {
                    DGG.Layer.open(sysInfo.basePath + "/sc/general/inprocess/openUpdateNode.html?id=" + id, {
                        title: '更新节点',
                        area: ['25%', '55%'],
                        btn: ''
                    });
                } else {
                    DGG.Layer.message(obj.msg, {icon: '2'});
                }
            },
            error: function () {
                DGG.Layer.message('出现未知错误，请联系管理员！', {icon: '2'});
            }
        });


    } else if (null != id && "" != id) {
        //判断该条数据弹出那个业态的界面
        $.ajax({
            type: 'POST',
            async: false,
            url: sysInfo.basePath + '/sc/general/inprocess/validationUpdateNode.do?id=' + id,
            dataType: 'JSON',
            success: function (obj) {
                if (obj.code === 0) {
                    DGG.Layer.open(sysInfo.basePath + "/sc/general/inprocess/openUpdateNode.html?id=" + id, {
                        title: '更新节点',
                        area: ['400px', '550px'],
                        btn: ''
                    });
                } else {
                    DGG.Layer.message(obj.msg, {icon: '2'});
                }
            },
            error: function () {
                DGG.Layer.message('出现未知错误，请联系管理员！', {icon: '2'});
            }
        });
    }
}


/**
 *  打开生产暂停
 *  2018/5/12  10:18
 *  孙上峰
 */
function suspendOrder(tableid, id) {
    if (null != tableid && "" != tableid) {
        var id = scCommonGetCheckedallproorder(tableid);
        if (id.length > 1) {
            DGG.Layer.message('只能选择一个订单！', {icon: '2'});
            return;
        }
        if (id.length == 0) {
            DGG.Layer.message('请选择一个订单！', {icon: '2'});
            return;
        }
        DGG.Layer.open(sysInfo.basePath + "/sc/general/inprocess/suspendOrderPage.html?id=" + id, {
            title: '生产暂停',
            area: ['500px', '430px'],
            btn: ''
        });
    } else {
        DGG.Layer.open(sysInfo.basePath + "/sc/general/inprocess/suspendOrderPage.html?id=" + id, {
            title: '生产暂停',
            area: ['500px', '430px'],
            btn: ''
        });
    }
}


/**
 * 打开退单
 */
function chargeBackOrder(tableid, id) {
    if (null != tableid && "" != tableid) {
        var id = scCommonGetCheckedallproorder(tableid);
        if (id.length > 1) {
            DGG.Layer.message('只能选择一个订单！', {icon: '2'});
            return;
        }
        if (id.length == 0) {
            DGG.Layer.message('请选择一个订单！', {icon: '2'});
            return;
        }
        DGG.Layer.open(sysInfo.basePath + "/sc/general/inprocess/chargeBackOrderPage.html?id=" + id, {
            title: '申请退单',
            area: ['400px', '300px'],
            btn: ''
        });
    } else {
        DGG.Layer.open(sysInfo.basePath + "/sc/general/inprocess/chargeBackOrderPage.html?id=" + id, {
            title: '申请退单',
            area: ['400px', '300px'],
            btn: ''
        });
    }
}


/**
 * 打开废单
 */
function abolishOrder(tableid, id) {
    if (null != tableid && "" != tableid) {
        var id = scCommonGetCheckedallproorder(tableid);
        if (id.length > 1) {
            DGG.Layer.message('只能选择一个订单！', {icon: '2'});
            return;
        }
        if (id.length == 0) {
            DGG.Layer.message('请选择一个订单！', {icon: '2'});
            return;
        }
        DGG.Layer.open(sysInfo.basePath + "/sc/general/inprocess/abolishOrderPage.html?id=" + id, {
            title: '申请废单',
            area: ['400px', '300px'],
            btn: ''
        });
    } else {
        DGG.Layer.open(sysInfo.basePath + "/sc/general/inprocess/abolishOrderPage.html?id=" + id, {
            title: '申请废单',
            area: ['400px', '300px'],
            btn: ''
        });
    }
}

/**
 * 打开移交
 */
function turnOverOrder(tableid, ids) {
    if (null != tableid && "" != tableid) {
        var ids = scCommonGetCheckedallproorder(tableid);
        if (ids.length == 0) {
            DGG.Layer.message('请选择一个订单！', {icon: '2'});
            return;
        }
        if (ids.length > 1) {
            DGG.Layer.open(sysInfo.basePath + "/sc/general/inprocess/turnOverOrderPage.html?ids=" + ids, {
                title: '批量移交',
                area: ['400px', '300px'],
                btn: ''
            });
        } else {
            DGG.Layer.open(sysInfo.basePath + "/sc/general/inprocess/turnOverOrderPage.html?ids=" + ids, {
                title: '移交',
                area: ['400px', '300px'],
                btn: ''
            });
        }

    } else {
        if (ids.length > 1) {
            DGG.Layer.open(sysInfo.basePath + "/sc/general/inprocess/turnOverOrderPage.html?id=" + ids, {
                title: '批量移交',
                area: ['400px', '300px'],
                btn: ''
            });
        } else {
            DGG.Layer.open(sysInfo.basePath + "/sc/general/inprocess/turnOverOrderPage.html?id=" + ids, {
                title: '移交',
                area: ['400px', '300px'],
                btn: ''
            });
        }
    }
}


/**
 * 审核
 * 2018/5/14  10:09
 * 孙上峰
 */
function auditOrder(tableid, id) {
    if (null != tableid && "" != tableid) {
        var id = scCommonGetCheckedallproorder(tableid);
        if (id.length > 1) {
            DGG.Warning('只能选择一个订单！');
            return;
        }
        if (id.length == 0) {
            DGG.Warning('请选择一个订单！');
            return;
        }
        DGG.loading();
        $.post(sysInfo.basePath + "/zzsc/audit/audit_order_page.html", {id: id[0]}, function (data) {
            dgg.Open({
                title: '审核退单',
                content: data,
                area: '400px',
                btn: ["确定", "取消"],
                yes: function () {
                    auditOrderUpdate(id[0]);
                }
            });
        }, 'html');
    } else {
        DGG.loading();
        $.post(sysInfo.basePath + "/zzsc/audit/audit_order_page.html", {id: id}, function (data) {
            dgg.Open({
                title: '审核退单',
                content: data,
                area: '400px',
                btn: ["确定", "取消"],
                yes: function () {
                    auditOrderUpdate(id);
                }
            });
        }, 'html');
    }
}

/**
 * 变更审核人
 * 孙上峰
 */
function updateFlowUser(tableid, id) {
    if (null != tableid && "" != tableid) {
        var id = scCommonGetCheckedallproorder(tableid);
        if (id.length > 1) {
            DGG.Warning('只能选择一个订单！');
            return;
        }
        if (id.length == 0) {
            DGG.Warning('请选择一个订单！');
            return;
        }
        DGG.loading();
        $.post(sysInfo.basePath + "/zzsc/audit/update_flow_user.html", {id: id[0]}, function (data) {
            dgg.Open({
                title: '变更审核人',
                content: data,
                area: '400px',
                btn: ["确定", "取消"],
                yes: function () {
                    updateAuditor(id[0]);
                }
            });
        }, 'html');
    } else {
        DGG.loading();
        $.post(sysInfo.basePath + "/zzsc/audit/update_flow_user.html", {id: id[0]}, function (data) {
            dgg.Open({
                title: '变更审核人',
                content: data,
                area: '400px',
                btn: ["确定", "取消"],
                yes: function () {
                    updateAuditor(id);
                }
            });
        }, 'html');
    }
}

/**
 * 添加协单人员
 * @param tableid
 * 孙上峰
 */
function addAssist(tableid, id) {
    if (null != tableid && "" != tableid) {
        var id = scCommonGetCheckedallproorder(tableid);
        if (id.length > 1) {
            DGG.Layer.message('只能选择一个订单！', {icon: '2'});
            return;
        }
        if (id.length == 0) {
            DGG.Layer.message('请选择一个订单！', {icon: '2'});
            return;
        }
        DGG.Layer.open(sysInfo.basePath + "/sc/general/inprocess/addAssistPage.html?id=" + id, {
            title: '添加协单人员',
            area: ['500px', '300px'],
            btn: ''
        });
    } else {
        DGG.Layer.open(sysInfo.basePath + "/sc/general/inprocess/addAssistPage.html?id=" + id, {
            title: '添加协单人员',
            area: ['500px', '300px'],
            btn: ''
        });
    }
}

/**
 *
 * @param id 生产订单id
 * @param type 废单1 退单2
 */
function abolishOrChargeBack(type) {
    var params = $('#abandonReasonTable').serializeObject();
    DGG.Layer.confirm("确认退/废订单吗？", {'title': '退/废订单'}, function () {
        DGG.Layer.loading.start();
        $.ajax({
            url: sysInfo.basePath + '/sc/general/inprocess/abandonReasonOrder.do?type=' + type,
            data: params,
            type: "POST",
            dataType: "json",
            success: function (obj) {
                DGG.Layer.loading.done();
                if (obj.code == 1) {
                    DGG.Layer.message(obj.msg, {icon: '2'}, function () {
                        window.parent.dataTableReload();
                    });
                } else {
                    DGG.Layer.message('操作成功！', {icon: '1'}, function () {
                        if (window.parent.dataTableReload) {
                            window.parent.dataTableReload();
                            parent.layer.closeAll();
                        } else {
                            var tabName = window.top.getName();
                            var beforeTableName = window.parent.document.getElementById('beforeTableName').value;
                            var beforeTableUrl = window.parent.document.getElementById('beforeTableUrl').value;
                            window.top.updateTab(beforeTableName, beforeTableUrl);
                            window.top.assignClose(tabName);
                        }
                    });
                }
            }
        });
    }, function () {

    });
}


//恢复办理弹出框
function openRestore(tableid, id) {
    if (null != tableid && "" != tableid) {
        var tableid = scCommonGetCheckedallproorder(tableid);
        if (tableid.length > 1) {
            DGG.Warning('只能选择一个订单！');
            return;
        }
        if (tableid.length == 0) {
            DGG.Warning('请选择一个订单！');
            return;
        }
        DGG.loading();
        $.post(sysInfo.basePath + "/zzsc/chargebackre_jected/restore_page.html", {id: tableid[0]}, function (data) {
            dgg.Open({
                title: '恢复办理',
                content: data,
                area: '20%',
                btn: ["确定", "取消"],
                yes: function () {
                    restore(tableid[0]);
                }
            });
        }, 'html');

    } else {
        DGG.loading();
        $.post(sysInfo.basePath + "/zzsc/chargebackre_jected/restore_page.html", {id: id}, function (data) {
            dgg.Open({
                title: '恢复办理',
                content: data,
                area: '20%',
                btn: ["确定", "取消"],
                yes: function () {
                    restore(id);
                }
            });
        }, 'html');
    }
}

/**
 * 恢复办理
 * @param tableId
 */
/*function restore(id, type) {
    if (id.length > 1) {
        DGG.Layer.message('只能选择一个订单！', {icon: '2'});
        return;
    }
    if (id.length == 0) {
        DGG.Layer.message('请选择一个订单！', {icon: '2'});
        return;
    }
    DGG.Layer.confirm("确认恢复订单吗？", {'title': '恢复办理'}, function () {
        DGG.Layer.loading.start();
        $.ajax({
            url: sysInfo.basePath + '/sc/general/abolishRejected/restoreOrder.do?type=' + id + '&type=' + type,
            type: "POST",
            dataType: "json",
            success: function (obj) {
                DGG.Layer.loading.done();
                if (obj.code != 0) {
                    DGG.Layer.message(obj.msg, {icon: '2'}, function () {
                        window.parent.dataTableReload();
                    });
                } else {
                    DGG.Layer.message('操作成功！', {icon: '1'}, function () {
                        window.parent.dataTableReload();
                        parent.layer.closeAll();
                    });
                }
            }
        });
    }, function () {

    });
 }*/

/**
 *  获取选择的订单id
 *  2018/5/9  10:09
 *  孙上峰
 **/
function scCommonGetCheckedallproorder(tableid) {
    var datas = $('#' + tableid).dataTableExtend('getCheck');
    var ids = [];
    for (var i = 0; i < datas.length; i++) {
        ids.push(datas[i].id);
    }
    return ids;
}
